/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.scrum4j.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 *
 * @author LOD
 */
@Entity
@Table(name="Projects")
public class Project implements Serializable
{
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long projectID = -1;

   @Column
   private String name;

   @Column
   private String description;

   @Column
   @Temporal(TemporalType.DATE)
   private Date startDate;

   @Column
   @Enumerated(EnumType.ORDINAL)
   private ProjectState currentState;

   /**
    * All userstorys, a story without a sprint dependency is backlog...
    */
   @OneToMany(mappedBy = "project", cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.EAGER)
   private List<UserStory> userStorys;

   @OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
   @Fetch(FetchMode.SELECT) //eager lad geht per join (default) nur über eine Ebene, daher hier Select
   private List<Sprint> sprints;

   public long  getProjectID()
   {
      return projectID;
   }

   public String getDescription()
   {
      return description;
   }

   public void setDescription(String description)
   {
      this.description = description;
   }

   public String getName()
   {
      return name;
   }

   public void setName(String name)
   {
      this.name = name;
   }

   public List<UserStory> getUserStorys()
   {
      if (userStorys == null)
      {
         userStorys = new LinkedList<UserStory>();
      }
      return userStorys;
   }

   public void setUserStories(List<UserStory> userStories)
   {
      getUserStorys().clear();
      for (UserStory userStory : userStories)
      {
         userStory.setProject(this);
         getUserStorys().add(userStory);
      }
   }

   public void addUserStories(UserStory... userStories)
   {
      for (UserStory userStory : userStories)
      {
         userStory.setProject(this);
         getUserStorys().add(userStory);
      }
   }

   public ProjectState getCurrentState()
   {
      return currentState;
   }

   public void setCurrentState(ProjectState currentState)
   {
      this.currentState = currentState;
   }

   public Date getStartDate()
   {
      return startDate;
   }

   public void setStartDate(Date startDate)
   {
      this.startDate = startDate;
   }

   public List<Sprint> getSprints()
   {
      if (sprints == null)
      {
         sprints = new LinkedList<Sprint>();
      }
      return sprints;
   }

   public void setSprints(List<Sprint> sprints)
   {
      getSprints().clear();
      for (Sprint sprint : sprints)
      {
         sprint.setProject(this);
         getSprints().add(sprint);
      }
   }

   public void addSprints(Sprint... sprints)
   {
      for (Sprint sprint : sprints)
      {
         sprint.setProject(this);
         getSprints().add(sprint);
      }
   }

   public void removeSprint(Sprint sprint1)
   {
      for (Iterator<Sprint> it = getSprints().iterator(); it.hasNext();)
      {
         Sprint sprint = it.next();
         if (sprint.getSprintID() == sprint.getSprintID())
         {
            it.remove();
         }
      }
   }
}
